package com.gh.ggkt.vod.controller;

import com.gh.Result;
import com.gh.ggkt.vod.service.VodService;
import com.gh.ggkt.vod.utils.ConstantPropertiesUtil;
import com.gh.ggkt.vod.utils.Signature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Random;

/**
 * @author Eric Fang
 * @date 2022/9/15 22:17
 */
@RestController
@RequestMapping("/admin/vod")
public class VodController {

    @Autowired
    private VodService vodService;

    @PostMapping("upload")
    public Result upload(MultipartFile file){
        String fileId = vodService.uploadVideo(file);
        return Result.ok(fileId);
    }

    @DeleteMapping("remove/{fileId}")
    public Result removeVideo(@PathVariable String fileId){
        vodService.removeVideo(fileId);
        return Result.ok(fileId);
    }

    /**
     * 返回客户端上传视频签名
     */
    @GetMapping("sign")
    public Result sign(){
        Signature sign = new Signature();
        // 设置 App 的云 API 密钥
        sign.setSecretId(ConstantPropertiesUtil.SECRETID);
        sign.setSecretKey(ConstantPropertiesUtil.SECRETKEY);
        sign.setCurrentTime(System.currentTimeMillis() / 1000);
        sign.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE));
        sign.setSignValidDuration(3600 * 24 * 2); // 签名有效期：2天
        try {
            String signature = sign.getUploadSignature();
            return Result.ok(signature);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
